home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / misc_lib / xgeneral.c < prev   
Encoding:
C/C++ Source or Header  |  1993-12-30  |  7.8 KB  |  317 lines

  1. /*****************************************************************************
  2. * replacer for the MSDOS functions.                         *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 1.0, Aug. 1991   *
  5. *****************************************************************************/
  6.  
  7. #include <math.h>
  8. #include <time.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12. #ifdef OS2GCC
  13. #define INCL_DOSPROCESS
  14. #include <os2.h>
  15. #endif /* OS2GCC */
  16. #ifdef SGINAP
  17. #include <limits.h>
  18. #endif /* SGINAP */
  19. #ifdef __WINNT__
  20. #include <windows.h>
  21. #else
  22. #include <sys/types.h>
  23. #if !(defined(AMIGA) && defined(__SASC))
  24. #include <sys/times.h>
  25. #include <sys/param.h>
  26. #endif /* !(AMIGA && __SASC) */
  27. #ifndef HZ
  28. #define HZ 60
  29. #endif /* HZ */
  30. #endif /* __WINNT__ */
  31.  
  32. #include "irit_sm.h"
  33. #include "imalloc.h"
  34.  
  35. /*****************************************************************************
  36. *   Routine to duplicate a string. Exists in some implementations.         *
  37. *****************************************************************************/
  38. char *IritStrdup(char *s)
  39. {
  40.     char
  41.     *p = IritMalloc(strlen(s) + 1);
  42.  
  43.     if (p == NULL)
  44.     return NULL;
  45.  
  46.     strcpy(p, s);
  47.  
  48.     return p;
  49. }
  50.  
  51. /*****************************************************************************
  52. *   Routine to force a process to sleep.                     *
  53. *****************************************************************************/
  54. void IritSleep(int MiliSeconds)
  55. {
  56. #ifdef USLEEP
  57.     usleep(MiliSeconds * 1000);
  58. #else
  59. #ifdef SGINAP
  60.     sginap((long) (MAX(CLK_TCK * MiliSeconds / 1000, 1)));
  61. #else
  62. #ifdef OS2GCC
  63.     DosSleep(MiliSeconds);
  64. #else
  65. #ifdef __WINNT__
  66.     Sleep(MiliSeconds);
  67. #else
  68.     int i;
  69.  
  70.     for (i = MiliSeconds * 1000; i > 0; i--);
  71. #endif /* __WINNT__ */
  72. #endif /* OS2GCC */
  73. #endif /* SGINAP */
  74. #endif /* USLEEP */
  75. }
  76.  
  77. /*****************************************************************************
  78. *   Routine to initialize the random number generator.                 *
  79. *****************************************************************************/
  80. void IritRandomInit(long Seed)
  81. {
  82. #ifdef RAND
  83.     srand(Seed);
  84. #else
  85. #ifdef RAND48
  86.     srand48(Seed);
  87. #else
  88.     srandom(Seed);
  89. #endif /* RAND48 */
  90. #endif /* RAND */
  91. }
  92.  
  93. /*****************************************************************************
  94. *   Routine to compute a random number in a specified range.             *
  95. *****************************************************************************/
  96. RealType IritRandom(RealType Min, RealType Max)
  97. {
  98.     long R;
  99.  
  100. #ifdef RAND
  101.     R = rand();
  102. #else
  103. #ifdef RAND48
  104.     R = lrand48();
  105. #else
  106.     R = random();
  107. #endif /* RAND48 */
  108. #endif /* RAND */
  109.  
  110. #if defined(OS2GCC) || defined(sgi)
  111.     return Min + (Max - Min) * (R & RAND_MAX) / ((double) RAND_MAX);
  112. #else
  113.     return Min + (Max - Min) * (R & 0x7fffffff) / ((double) 0x7fffffff);
  114. #endif /* OS2GCC || sgi */
  115. }
  116.  
  117. /*****************************************************************************
  118. *   Routine to compute the cpu time of the running process.             *
  119. *   If Reset, then time count is cleared to zero.                 *
  120. *****************************************************************************/
  121. RealType IritCPUTime(int Reset)
  122. {
  123.     static RealType
  124.     LastTime = 0.0;
  125.     RealType Time;
  126.  
  127.     if (Reset) {
  128. #ifdef TIMES
  129.     struct tms tim;
  130.  
  131.     times(&tim);
  132.  
  133.     LastTime = (tim.tms_utime + tim.tms_stime) / ((RealType) HZ);
  134. #else
  135.     LastTime = time(NULL);                /* No real timing routines!? */
  136. #endif /* __WINNT__ */
  137.  
  138.     return 0.0;
  139.     }
  140.  
  141. #ifdef TIMES
  142.     {
  143.     struct tms tim;
  144.  
  145.     times(&tim);
  146.  
  147.     Time = (tim.tms_utime + tim.tms_stime) / ((RealType) HZ) - LastTime;
  148.     }
  149. #else
  150.     Time = time(NULL) - LastTime;
  151. #endif /* TIMES */
  152.  
  153.     return Time;
  154. }
  155.  
  156. /*****************************************************************************
  157. *   Routine to create and return a string describing current date and time.  *
  158. *****************************************************************************/
  159. char *IritRealTimeDate(void)
  160. {
  161.     static char StrTime[LINE_LEN];
  162.     int i;
  163.     long
  164.     t = (long) time(NULL);
  165.  
  166.     strncpy(StrTime, ctime(&t), LINE_LEN - 1);
  167.  
  168.     /* Remove trailing EOL. */
  169.     for (i = strlen(StrTime) - 1; StrTime[i] < ' ' && i >= 0; i--);
  170.     StrTime[i + 1] = 0;
  171.  
  172.     return StrTime;
  173. }
  174.  
  175. #ifndef AMIGA
  176. /*****************************************************************************
  177. *   Routine to move a block in memory. Unlike memcpy/bcopy, this routine     *
  178. * should support overlaying blocks. This stupid implemetation will copy it   *
  179. * twice - to a temporary block and back again. The temporary block size will *
  180. * be allocated by demand.                             *
  181. *****************************************************************************/
  182. void movmem(VoidPtr Src, VoidPtr Dest, int Len)
  183. {
  184.     VoidPtr
  185.     p = IritMalloc(Len);
  186.  
  187.     memcpy(p, Src, Len);
  188.     memcpy(Dest, p, Len);
  189.  
  190.     IritFree(p);
  191. }
  192. #endif /* AMIGA */
  193.  
  194. /*****************************************************************************
  195. *   Routine to concat a full path to a given name - used in non MSDOS        *
  196. * environment only, and in that case assumes path is in IRIT_PATH variable.  *
  197. *****************************************************************************/
  198. char *searchpath(char *Name)
  199. {
  200.     static char FullPath[LINE_LEN_LONG];
  201.     char *p;
  202.  
  203.     if ((p = getenv("IRIT_PATH")) != NULL) {
  204.     strcpy(FullPath, p);
  205.     strcat(FullPath, Name);
  206.     }
  207.     else {
  208.     static int Printed = FALSE;
  209.     
  210.     strcpy(FullPath, Name);
  211.  
  212.     if (!Printed) {
  213.         fprintf(stderr,
  214.             "IRIT_PATH env. not set. Only current directory is being searched.\n");
  215.         Printed = TRUE;
  216.     }
  217.     }
  218.     return FullPath;
  219. }
  220.  
  221. #ifdef STRICMP
  222. /*****************************************************************************
  223. *   Routine to compare two strings, ignoring case, up to given length.       *
  224. *****************************************************************************/
  225. int strnicmp(char *s1, char *s2, int n)
  226. {
  227.     /* Use to be simply 'return strncasecmp(s1, s2, n);' but seems that some */
  228.     /* unix systems (sun4?) does have it so here it is explicitly.         */
  229.     /* Written by Reiner Wilhelms <reiner@shs.ohio-state.edu>.             */
  230.     int i;
  231.     char c1, c2;
  232.  
  233.     for (i = 0; i < n; i++) {
  234.         if (islower(s1[i]))
  235.         c1 = toupper(s1[i]);
  236.         else
  237.         c1 = s1[i];
  238.         if (islower(s2[i]))
  239.         c2 = toupper(s2[i]);
  240.         else
  241.         c2 = s2[i];
  242.  
  243.         if (c1 != c2) {
  244.         if (c1 > c2)
  245.         return 1;
  246.             if (c1 < c2)
  247.         return -1;
  248.     }
  249.     }
  250.     return 0;
  251. }
  252.  
  253. /*****************************************************************************
  254. *   Routine to compare two strings, ignoring their case.             *
  255. *****************************************************************************/
  256. int stricmp(char *s1, char *s2)
  257. {
  258.     int i;
  259.     char *u1, *u2;
  260.  
  261.     if (s1 == NULL)
  262.     return s2 != NULL;
  263.     else if (s2 == NULL)
  264.     return 1;
  265.  
  266.     u1 = IritStrdup(s1);
  267.     u2 = IritStrdup(s2);
  268.  
  269.     for (i = 0; i < (int) strlen(u1); i++)
  270.     if (islower(u1[i]))
  271.         u1[i] = toupper(u1[i]);
  272.     for (i = 0; i < (int) strlen(u2); i++)
  273.     if (islower(u2[i]))
  274.         u2[i] = toupper(u2[i]);
  275.  
  276.     i = strcmp(u1, u2);
  277.  
  278.     IritFree(u1);
  279.     IritFree(u2);
  280.  
  281.     return i;
  282. }
  283. #endif /* STRICMP */
  284.  
  285. #ifdef STRSTR
  286. /*****************************************************************************
  287. *   Routine to search for pattern (no regular expression) in s. Returns      *
  288. * address in s of first occurance of patern, NULL if non found.             *
  289. *****************************************************************************/
  290. char *strstr(char *s, char *Pattern)
  291. {
  292.     int Len = strlen(Pattern);
  293.     char
  294.     *p = (char *) s;
  295.  
  296.     while (p = strchr(p, Pattern[0]))
  297.     if (strncmp(p, Pattern, Len) == 0)
  298.         return p;
  299.     else
  300.         p++;
  301.  
  302.     return NULL;
  303. }
  304. #endif /* STRSTR */
  305.  
  306. #ifdef GETCWD
  307. /*****************************************************************************
  308. *   Get current working directory - BSD4.3.                     *
  309. *****************************************************************************/
  310. char *getcwd(char *s, int Len)
  311. {
  312.     getwd(s);
  313.  
  314.     return s;
  315. }
  316. #endif /* GETCWD */
  317.